package com.unity3d.sqlite;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    UserDao userDao;
    UserDaoDatabase userDaoDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //1.和数据库建立连接
    userDaoDatabase= Room.databaseBuilder(this,UserDaoDatabase.class,"Mytest")
            .allowMainThreadQueries()//允许在主线程调用
            .build();

    //2.返回的不是一个接口对象，返回的是一个接口的实现类  是运行期间动态生成的
    userDao=userDaoDatabase.getUserDao();

    //3.操作数据库
        //增加数据
    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            User1 user1=new User1();
            user1.userName="starry";
            user1.age=20;
            user1.passWord="lx0411";
            userDao.insertUser(user1);//接口的方法是不能够调用?为什么这里可以调用
        }
    });

        //查询数据
    findViewById(R.id.button2).setOnClickListener(
        (view)->{
            List<User1> user1List=userDao.findAllUserById(1);
            for(User1 user1:user1List){
                Log.i("user1", "id: "+user1.id+" name: "+user1.userName);
            }

        }
    );

    //删除数据
    findViewById(R.id.button3).setOnClickListener(
        (view)->{
            User1 user1=new User1();
            user1.id=2;
            userDao.deleteUser(user1);
        }
    );



    }
}